﻿using PrjAssessModels.BaseModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PrjAssess.Models.Service.UserLog
{
    public class UserDataHelper
    {
        public static User ValidateUser(string userCode, string userPwd)
        {
            PrjAssessDb db = new PrjAssessDb();
            User rec = db.Users.Where(c => c.UserCode == userCode && c.UserPwd == userPwd).FirstOrDefault();
            return rec;
        }

        public static User GetUserByCode(string userCode)
        {
            PrjAssessDb db = new PrjAssessDb();
            return db.Users.Where(c => c.UserCode == userCode).FirstOrDefault();
        }
        public static int AddUser(User user)
        {
            PrjAssessDb db = new PrjAssessDb();
            User tempUser = UserDataHelper.GetUserByCode(user.UserCode);
            //首先判断是否已存在该用户
            if (tempUser != null)
            {
                return -1;
            }
            db.Users.Add(user);
            return db.SaveChanges();
        }
        public static int DeleteUser(User user)
        {
            PrjAssessDb db = new PrjAssessDb();
            User tempRec = db.Users.Find(user.Id);
            if (tempRec == null)
            {
                return -1;
            }
            db.Users.Remove(tempRec);
            return db.SaveChanges();
        }

 
    }
}